package com.jee.support.mongodb;

import java.net.UnknownHostException;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class TestMongoDb {

		public static void main(String[] args) {
			
			try {
				Mongo mongo = new Mongo("localhost");
				Datastore datastore = new Morphia().createDatastore(mongo, "springmongo");
				Address address = new Address();
				address.setCity("hangzhou");
				address.setCountry("American");
				address.setPostCode("2011");
				Hotel hotel = new Hotel();
				hotel.setName("spring");
				hotel.setAddress(address);
				
				datastore.save(hotel);
				
				Query<Hotel>query = datastore.createQuery(Hotel.class).field("name").equal("spring");
				Hotel get_hotel = query.get();
				System.out.println("name = " + get_hotel.getName() + ", address=" + get_hotel.getAddress().getCountry());
				
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MongoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}
